﻿//-----------------------------------------------------------------------
// <copyright file="DataContextInitializer.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//---------------------------------------------------------------------
namespace Deixei.Domain
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Threading;
    using System.Web;
    using System.Web.Security;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using Deixei.Domain.Service;
    using Deixei.Infrastructure;
    using System.Data.SqlClient;

    
    public class DataContextInitializer : DropCreateDatabaseAlways<DataContext>
    //public class DataContextInitializer : DropCreateDatabaseIfModelChanges<DataContext>
    {
        // TODO: Add Navigation Entities (Area and Controller) to database when initialization starts
        // CHECK: file General Extensions MenuBar ?!!!!
        //

        private void LoadNavigation(DataContext context)
        {
            WebAppPortfolio App = new WebAppPortfolio() { Name = "Deixei", Description = "Deixei Web Application", Url = "http://localhost:55055/", Created = DateTime.Now, CreatedBy = "DataContextInitializer" };
            context.WebAppsPortfolio.Add(App);

            NavigationArea navAreaHome = new NavigationArea() { WebAppPortfolio = App, GroupName = "Start", Area = "Home", Description = "Home", Url = "/", Created = DateTime.Now, CreatedBy = "DataContextInitializer" };
            context.NavigationAreas.Add(navAreaHome);


            context.NavigationControllers.Add(new NavigationController() { NavigationArea = navAreaHome, Description = "Home", Area = "", Controller = "Home", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = navAreaHome, Description = "About", Area = "", Controller = "Home", Action = "About", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = navAreaHome, Description = "Contact", Area = "", Controller = "Home", Action = "Contact", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });




            NavigationArea a1 = context.NavigationAreas.Add(new NavigationArea() { WebAppPortfolio = App, GroupName = "Areas", Area = "CustomerService", Description = "Customer Service", Url = "/CustomerService/Accounts/Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            NavigationArea a2 = context.NavigationAreas.Add(new NavigationArea() { WebAppPortfolio = App, GroupName = "Areas", Area = "Marketing", Description = "Marketing", Url = "/Marketing/Segmentation/Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            NavigationArea a3 = context.NavigationAreas.Add(new NavigationArea() { WebAppPortfolio = App, GroupName = "Areas", Area = "Sales", Description = "Sales", Url = "/Sales/Customer/Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            NavigationArea a4 = context.NavigationAreas.Add(new NavigationArea() { WebAppPortfolio = App, GroupName = "Areas", Area = "Social", Description = "Social", Url = "/Social/Facebook/Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });

            NavigationArea a5 = context.NavigationAreas.Add(new NavigationArea() { WebAppPortfolio = App, GroupName = "Demos", Area = "Admin", Description = "Administration", Url = "/Admin/Portfolio/Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            NavigationArea a6 = context.NavigationAreas.Add(new NavigationArea() { WebAppPortfolio = App, GroupName = "Demos", Area = "Demos", Description = "Demos", Url = "/Demos/Bootstrap/Components", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            NavigationArea a7 = context.NavigationAreas.Add(new NavigationArea() { WebAppPortfolio = App, GroupName = "Demos", Area = "MarcioParente", Description = "Marcio Parente", Url = "/MarcioParente/CV/Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });

            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a1, Description = "Accounts", Area = "CustomerService", Controller = "Accounts", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a1, Description = "Cases", Area = "CustomerService", Controller = "Cases", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a1, Description = "Contracts", Area = "CustomerService", Controller = "Contracts", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a1, Description = "Knowledge", Area = "CustomerService", Controller = "Knowledge", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a1, Description = "Scheduling", Area = "CustomerService", Controller = "Scheduling", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });

            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a3, Description = "Customer", Area = "Sales", Controller = "Customer", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a3, Description = "Leads", Area = "Sales", Controller = "Leads", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a3, Description = "Opportunities", Area = "Sales", Controller = "Opportunities", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a3, Description = "Pipeline", Area = "Sales", Controller = "Pipeline", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a3, Description = "Planning", Area = "Sales", Controller = "Planning", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a3, Description = "Timesavers", Area = "Sales", Controller = "Timesavers", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });

            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a5, Description = "Portfolio", Area = "Admin", Controller = "Portfolio", Action = "Index", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });
            context.NavigationControllers.Add(new NavigationController() { NavigationArea = a6, Description = "Bootstrap", Area = "Demos", Controller = "Bootstrap", Action = "Components", Created = DateTime.Now, CreatedBy = "DataContextInitializer" });

            context.SaveChanges();
        }
                
            ////    default:
            ////        item.Append(
            ////            htmlHelper.MenuItem("Home", "Index", "Home"),
            ////            htmlHelper.MenuItem("Components Demos", "Components", "Bootstrap", "Demos"),
            ////            htmlHelper.MenuItem("MP CV", "Index", "CV", "MarcioParente"),
            ////            htmlHelper.MenuItem("Customer Sales", "Index", "Customer", "Sales"),
            ////            htmlHelper.MenuItem("About", "About", "Home"),
            ////            htmlHelper.MenuItem("Contact", "Contact", "Home"),
            ////            htmlHelper.MenuItem("Test", "Test", "Home"),
            ////            htmlHelper.MenuItem("Values", "Values", "Home"));
            ////        break;
            ////}

        //public void InitializeDatabase(DataContext context)
        //{
        //    if ( context.Database.Exists() )
        //    {
        //        SqlConnection.ClearAllPools();
        //    }
        //    //LOG InitializeDatabase
        //    base.InitializeDatabase(context);
        //}

        protected override void Seed(DataContext context)
        {
            LoadNavigation(context);

            Membership.ApplicationName = "DEIXEI";
            MembershipCreateStatus Status;
            Membership.CreateUser("Marcio.Parente", "123456", "Marcio.Parente@deixei.com", null, null, true, out Status);
            Roles.CreateRole("Admin");
            Roles.AddUserToRole("Marcio.Parente", "Admin");
            
            // LOG Seed
            base.Seed(context);
        }

    }
}
